package com.neu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.MainActivity;
import com.neu.R;
import com.neu.bean.Result;
import com.neu.util.VolleyUtil;
import com.neu.view.App;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText txtUserName;
    private EditText txtPwd;
    private Button btnLogin;
    private Button btnReg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        txtUserName = (EditText) findViewById(R.id.txtUserName);
        txtPwd = (EditText) findViewById(R.id.txtPwd);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnReg = (Button) findViewById(R.id.btnReg);

        btnLogin.setOnClickListener(this);
        btnReg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnLogin:
                //调用登录方法
                submit();
                break;
            case R.id.btnReg:
                //跳转到注册页面
                Intent intent=new Intent(this,RegActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void submit() {
        // validate
        String txtUserNameString = txtUserName.getText().toString().trim();
        if (TextUtils.isEmpty(txtUserNameString)) {
            Toast.makeText(this, "txtUserNameString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String txtPwdString = txtPwd.getText().toString().trim();
        if (TextUtils.isEmpty(txtPwdString)) {
            Toast.makeText(this, "txtPwdString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        doLogin();

    }

    //登录方法
    private void doLogin() {
        Map<String,String> map=new HashMap<>();
        map.put("username",txtUserName.getText().toString());
        map.put("password",txtPwd.getText().toString());
        final String url="/prod-api/api/login";

        VolleyUtil.request(Request.Method.POST, url, map, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Gson gson=new Gson();
                Result result=gson.fromJson(json,Result.class);
                if(200==result.getCode()){//登录成功
                    //获得Token
                    VolleyUtil.TOKEN=result.getToken();
                    Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);

                    App.sRequsetQueue.getCache().remove(VolleyUtil.BASE_URL+url);
                    finish();
                }else{
                    App.sRequsetQueue.getCache().remove(VolleyUtil.BASE_URL+url);
                    Toast.makeText(App.context,"用户名密码错误",Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.sRequsetQueue.cancelAll(this);
    }
}